from rest_framework import serializers
from tenant import models
# 租户编码在后端验证唯一性，必须以H或者Y开头
def validate_tenant_code(value):
    if not value.startswith('H') and not value.startswith('Y'):
        raise serializers.ValidationError('租户编码必须以H或者Y开头')
    return value

class TenType(serializers.ModelSerializer):
    class Meta:
        model = models.TenType
        fields = '__all__'
class Tenant(serializers.ModelSerializer):
    dname=serializers.SerializerMethodField()
    def get_dname(self,obj):
        return obj.type.name
    status=serializers.SerializerMethodField()
    def get_status(self,obj):
        if obj.status:
            return '开启'
        else:
            return '关闭'
    class Meta:
        model = models.Tenant
        fields = '__all__'
        extra_kwargs = {
            'tsno': {
                'validators': [validate_tenant_code]
            }
        }
